﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TrainTicketBookingSystem
{
    public partial class AdminerForm : Form
    {
        private CarriageService carriageService = new CarriageService();
        private StationService stationService = new StationService();
        private TrainSequenceService trainSequenceService = new TrainSequenceService();
        private List<Station> stationsList = null;
        private List<TrainSequence> trainSequencesList = null;
        private DataGridViewRow selectedRow = null;
        public AdminerForm()
        {
            InitializeComponent();
            stationsList = stationService.FindAllStation();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (trainSequencesList != null)
                trainSequencesList.Clear();

            dataTable.Rows.Clear();

            DateTime LaunchTime = dateTimePicker.Value;
            string StartStation = comboBoxStartStation.Text;
            string EndStation = comboBoxEndStation.Text;
            trainSequencesList = trainSequenceService.FindTrainSqeuenceByTimeAndStation(LaunchTime,StartStation,EndStation);

            this.textBoxCount.Text = trainSequencesList.Count.ToString();
            ShowInTable();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AdminerForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoginFrom.loginFrom.Show();
        }

        private void AdminerForm_Load(object sender, EventArgs e)
        {

        }

        private void ShowInTable()
        {
            if(trainSequencesList.Count == 0)
            {
                return;
            }
            this.dataTable.RowCount = trainSequencesList.Count;

            for (int i = 0; i < trainSequencesList.Count; i++)
            {
                TrainSequence ts = trainSequencesList[i];
                this.dataTable[0,i].Value = ts.TrainNum;
                this.dataTable[1,i].Value = ts.TrainId;
                this.dataTable[2,i].Value = comboBoxStartStation.Text;
                this.dataTable[3,i].Value = comboBoxEndStation.Text;
                this.dataTable[4,i].Value = ts.LaunchTime;
                this.dataTable[5,i].Value = ts.ArriveTime;
                this.dataTable[6,i].Value = ts.TravelTime;
                this.dataTable[7,i].Value = carriageService.FindHighSeatNumByTrainId(ts.TrainId);
                this.dataTable[8,i].Value = carriageService.FindLowSeatNumByTrainId(ts.TrainId);
            }
        }

        private void BtnCreate_Click(object sender, EventArgs e)
        {
            this.dataTable.RowCount += 1;
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void BtnDelete_Click(object sender, EventArgs e)
        {
            if(selectedRow == null)
            {
                MessageBox.Show("请先选中要删除的行！");
                return;
            }

            if (MessageBox.Show("确定要删除这一行吗？", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                dataTable.Rows.Remove(selectedRow);
                selectedRow = null; // 删除后清空记录的行
            }
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            DataTable2TrainSequenceList();

            DateTime LaunchTime = dateTimePicker.Value;
            string StartStation = comboBoxStartStation.Text;
            string EndStation = comboBoxEndStation.Text;

            trainSequenceService.UpdateByTimeAndStation(LaunchTime,StartStation,EndStation,trainSequencesList);
        }

        private void DataTable2TrainSequenceList()
        {
            if(trainSequencesList != null)
            {
                trainSequencesList.Clear();
            }

            for(int i = 0; i < dataTable.RowCount; i++)
            {
                if (dataTable[0, i].Value == null || dataTable[0,i].Value.ToString().Trim() == "")
                {
                    continue;
                }

                string TrainNum = dataTable[0,i].Value.ToString().Trim();
                int TrainId = int.Parse(dataTable[1, i].Value.ToString());
                int StartStationId = stationService.FindStationIdByName(dataTable[2,i].Value.ToString());
                int EndStationId = stationService.FindStationIdByName(dataTable[3,i].Value.ToString());
                DateTime LaunchTime = DateTime.Parse(dataTable[4,i].Value.ToString());
                DateTime ArrivalTime = DateTime.Parse(dataTable[5,i].Value.ToString());
                TimeSpan TravelTime = LaunchTime - ArrivalTime;

                trainSequencesList.Add(new TrainSequence(i,TrainNum,TrainId,StartStationId,EndStationId,LaunchTime,ArrivalTime,TravelTime));
            }

            return;
        }

        private void dataTable_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                selectedRow = dataTable.Rows[e.RowIndex];
            }
        }
    }
}
